package com.ckzp.jfinal.weixin.controller;

import com.ckzp.core.json.JsonResult;
import com.ckzp.core.model.Datas;
import com.ckzp.jfinal.base.BaseController;
import com.ckzp.jfinal.base.PageSearch;
import com.ckzp.jfinal.form.FormData;
import com.ckzp.jfinal.form.FormSelect;
import com.ckzp.jfinal.interceptor.Admins;
import com.ckzp.jfinal.interceptor.Roles;
import com.ckzp.jfinal.weixin.model.WxMember;
import com.ckzp.jfinal.weixin.model.WxMessage;
import com.ckzp.jfinal.weixin.model.WxTemplet;
import com.ckzp.jfinal.weixin.util.WxMessageUtil;
import com.ckzp.jfinal.weixin.validate.WxTempletValidator;
import com.ckzp.plugins.layui.LayuiData;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * Created by 51594 on 2021/6/2.
 */
public class WxMessageController extends BaseController {

    @Roles(roles = "system")
    public void index() {
        renderText("默认");
    }

    /**
     * 列表
     */
    @Roles(roles = "system")
    public void WxMessageList() {
        FormData fd = FormData.dao();
        FormSelect fs = FormSelect.n().setReadOnly(false);
        fs.setCanNull(true).setDefaultString("==请选择==").setName("status").setSource("`0`待发送`1`发送成功`2`发送失败`");
        fd.addSelect("status", fs.showHtml());
        setAttr("form", fd);
        render("WxMessageList.html");
    }

    /**
     * 列表数据提供API
     */
    @Roles(roles = "system")
    public void WxMessageListDataApi() {
        PageSearch ps = PageSearch.n().init(getRequest(), "wx_message")
                .orderby(getRequest(), "open_id desc");
        Page<WxMessage> pages = ps.paginate(WxMessage.dao);
        FormSelect fs = FormSelect.n().setSource("`0`待发送`1`发送成功`2`发送失败`").setReadOnly(true);
        List datalist = pages.getList();
        for (int k = 0; k < datalist.size(); k++) {
            WxMessage st = (WxMessage) datalist.get(k);
            st.put("status_str", fs.showHtml(st.mod.getStr("status")));
        }
        renderJson(LayuiData.newData().setData(datalist).setCount(pages.getTotalRow()));
    }


    /**
     * 列表
     */
    @Roles(roles = "system")
    public void WxTempletList() {
        FormData fd = FormData.dao();
        setAttr("form", fd);
        render("WxTempletList.html");
    }

    /**
     * 列表数据提供API
     */
    @Roles(roles = "system")
    public void WxTempletListDataApi() {
        PageSearch ps = PageSearch.n().init(getRequest(), "wx_templet")
                .orderby(getRequest(), "sindex desc");
        Page<WxTemplet> pages = ps.paginate(WxTemplet.dao);
        List datalist = pages.getList();
        for (int k = 0; k < datalist.size(); k++) {
            WxTemplet st = (WxTemplet) datalist.get(k);
        }
        renderJson(LayuiData.newData().setData(datalist).setCount(pages.getTotalRow()));
    }

    /**
     * 新增微信模板Page
     */
    @Admins(roles = "system", name = "新增微信模板页面", log = false)
    public void AddWxTemplet() {
        FormData fd = FormData.dao().setActions("ADD");
        setAttr("temp", new WxTemplet());
        setAttr("form", fd);
        render("ModifyWxTemplet.html");
    }

    /**
     * 修改微信模板页面
     * fd.addSelect("", SysDataCache.getFormSelect("").setLayFilter("").showHtml(o.mod.getStr("")));
     */
    @Admins(roles = "system", name = "修改微信模板页面", log = false)
    public void EditWxTemplet() {
        String temp_id = getPara("temp_id", "");
        WxTemplet o = WxTemplet.dao.findById(temp_id);
        FormData fd = FormData.dao().setActions("EDIT");
        setAttr("temp", new WxTemplet());
        if (o != null) {

        }
        setAttr("form", fd);
        setAttr("temp", o);
        render("ModifyWxTemplet.html");
    }

    /**
     * 修改微信模板Api
     */
    @Before(WxTempletValidator.class)
    @Admins(roles = "system", name = "修改微信模板Api", log = true)
    public void ModifyWxTempletApi() {
//取得数据
        Datas data = getDatas();
        String temp_id = data.getStr("temp_id");//主键
        String actions = data.getStr("actions");//action
        if ("EDIT".equals(actions)) {//修改
            boolean results = WxTemplet.dao.findById(temp_id)
                    .set("temp_title", data.getStr("temp_title"))
                    .set("sindex", data.getInt("sindex"))
                    .set("temp_desc", data.getStr("temp_desc"))

                    .update();
            renderJson(Json(results, "修改"));
        } else {//添加
            boolean results = new WxTemplet()
                    .set("temp_title", data.getStr("temp_title"))
                    .set("sindex", data.getInt("sindex"))
                    .set("temp_desc", data.getStr("temp_desc"))

                    .save();
            renderJson(Json(results, "添加"));
        }
    }

    /**
     * 修改微信模板字段Api
     * demo:
     * fd.addSelect("", SysDataCache.getFormSelect("").setLayFilter("").showHtml(o.mod.getStr("")));
     */
    @Admins(roles = "system", name = "修改微信模板字段Api", log = true)
    public void ModifyWxTempletFieldApi() {
        String temp_id = getParaStr("key_id", "");
        String field = getPara("field", "");
        String value = getPara("values", "");
        WxTemplet o = WxTemplet.dao.findById(temp_id);
        if (o != null) {
            boolean status = o.set(field, value).update();
            renderJson(JsonResult.n(status ? 0 : 9, "修改" + (status ? "成功" : "失败")));
        } else {
            renderJson(JsonResult.n(0, "修改成功"));
        }
    }


    /**
     * 删除微信模板Api
     */
    @Admins(roles = "system", name = "删除微信模板Api", log = true)
    public void DelWxTempletApi() {
        int temp_id = getInt("temp_id", 0);
        WxTemplet temp = WxTemplet.dao.findById(temp_id);
        if (temp == null) {
            renderJson(JsonResult.n(9, "记录不存在"));
        } else if (temp.delete()) {
            renderJson(JsonResult.n(0, "删除成功"));
        } else {
            renderJson(JsonResult.n(9, "删除失败"));
        }
///////////////////////////////////
    }


    /**
     *
     */
    public void SendMessageApi() {
        int ids = getParaInt("ids", 0);
        if (WxMessageUtil.n().sendWxMessage(ids)) {
            renderJson(JsonResult.n(0, "发送成功"));
        } else {
            renderJson(JsonResult.n(9, "发送失败"));
        }
    }
}

